import Vue from 'vue'
import VueRouter from 'vue-router'
import notification from "@/utils/notification";
import PaperStuView from "@/views/examination/student/PaperStuView";
import AnswerListStuView from "@/views/examination/student/AnswerListStuView";
import ExamListStuView from "@/views/examination/student/ExamListStuView";
import PaperView from "@/views/examination/teacher/ItemBank/PaperView";
import ExamListView from "@/views/examination/teacher/ExamListView";
import ManageClassView from "@/views/examination/teacher/ManageClassView";
import WorkView from "@/views/WorkView";
import AnswerPaperStuView from "@/views/examination/student/AnswerPaperStuView";
import CorrectAnswerPaperListView from "@/views/examination/teacher/CorrectAnswerPaperView";
import ExamStatisticsView from "@/views/examination/teacher/statistics/ExamStatisticsView";
import HomeView from "@/views/examination/teacher/HomeView";
import QuestionMakerView from "@/views/examination/teacher/ItemBank/QuestionMakerView";
import BankQuestionListView from "@/views/examination/teacher/ItemBank/BankQuestionListView";
import PaperMakerView from "@/views/examination/teacher/ItemBank/PaperMakerView";


Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}



const routes = [
    {
        path: '/',
        name: 'IndexView',
        component: () => import("../views/IndexView"),
    },
    {
        path: '/login',
        name: 'login',
        component: () => import("../views/LoginView"),
    },
    {
        path: '/work',
        name: 'WorkView',
        component: WorkView,
        children: [
            ///////////////////老师///////////////////
            {
                path: "/work/home",
                name: "HomeView",
                component: HomeView
            },
            {
                path: "/work/mangeClass",
                name: "ManageClassView",
                component: ManageClassView
            },
            {
                path: "/work/examList",
                name: "ExamListView",
                component: ExamListView
            },
            {   path: "/work/paper",
                name: "PaperView",
                component: PaperView
            },
            {
                path: "/work/questionMaker",
                name: "QuestionMakerView",
                component: QuestionMakerView
            },
            {
                path: "/work/examStatistics",
                name: "ExamStatisticsView",
                component: ExamStatisticsView
            },
            {
                path: "/work/bankQuestionList",
                name: "BankQuestionListView",
                component: BankQuestionListView
            },
            {
                path: "/work/paperMaker",
                name: "PaperMakerView",
                component: PaperMakerView
            },

            ///////////////////学生///////////////////
            {
                path: "/work/examListStu",
                name: "ExamListStuView",
                component: ExamListStuView
            },
            {
                path: "/work/answerListStu",
                name: "AnswerListStuView",
                component: AnswerListStuView
            },


        ]
    },
    {
        path: '/PaperStu/:info',
        name: 'PaperStuView',
        component: PaperStuView,
        props: true,
    },
    {
        path: '/AnswerPaperStu/:answerPaper',
        name: 'AnswerPaperStuView',
        component: AnswerPaperStuView,
        props: true,
    },
    {
        path: "/CorrectAnswerPaper/:exam",
        name: "CorrectAnswerPaperView",
        component: CorrectAnswerPaperListView,
        props: true,
    },

]

const router = new VueRouter({
    // mode: 'history',
    routes
})

router.beforeEach((to, from, next) => {
    // 1. 判断是否登录页面
    // 是登录页面
    if (to.path == '/' || to.path == '/login') {
        next()
    } else {
        // 2.不是登录页面
        // 判断 是否登录过
        let token = localStorage.getItem('user')
        if (token == null) {

            next('/login');
            notification.ntfErr("错误", "身份信息失效，请重新登录");
        } else {
            next();
        }
    }
})

export default router
